//
//  RdbTabberController.swift
//  rdb-led-banner
//
//  Created by GJC on 2025/4/21.
//

import UIKit
import SnapKit

class RdbTabberController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let blurEffect = UIBlurEffect(style: .dark)
        let blurView = UIVisualEffectView(effect: blurEffect)
        self.tabBar.addSubview(blurView)
        
        blurView.snp.makeConstraints{
            $0.edges.equalToSuperview()
        }
        
        self.reloadData()
        NotificationCenter.default.addObserver(self, selector: #selector(languageChange), name: .rdbLanguageChange, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(themeChange), name: .rdbThemeColorChange, object: nil)
    }
    
    @objc func languageChange(){
        self.reloadData()
    }
    @objc func themeChange(){
        self.reloadData()
    }
    
    func reloadData(){
        let tabbarAppearance = UITabBarAppearance()
        let itemAppearance = UITabBarItemAppearance(style: .stacked)
        itemAppearance.normal.titleTextAttributes = [NSAttributedString.Key.font: UIFont.regular(12)]
        itemAppearance.selected.titleTextAttributes = [NSAttributedString.Key.font: UIFont.semibold(12)]
        if UIDevice.safeAreaTop() > 0 {
            itemAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
            itemAppearance.selected.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
        }
        tabbarAppearance.stackedLayoutAppearance = itemAppearance
        tabbarAppearance.backgroundEffect = nil
        
        self.tabBar.tintColor = RdbColor.shared.theme.color
        self.tabBar.unselectedItemTintColor = UIColor("#B4B4B4")
        self.tabBar.standardAppearance = tabbarAppearance
        if #available(iOS 15.0, *) {
            self.tabBar.scrollEdgeAppearance = tabbarAppearance
        }
        let modelList = self.getTabbarItems().kj.modelArray(RdbTabbarModel.self)
        if self.viewControllers == nil {
            var controllerArr: Array<RdbNavigationController> = Array()
            modelList.forEach{ model in
                let controller = controllerWithClassName(className: model.controller)
                let navigation = RdbNavigationController(rootViewController: controller)
                controllerArr.append(navigation)
            }
            self.viewControllers = controllerArr
        }
        self.configTabbarItem(modelList)
    }
    
    func configTabbarItem(_ modelList: [RdbTabbarModel]){
        for index in 0 ..< modelList.count {
            guard let navigation = self.viewControllers?[index] as? RdbNavigationController else { return }
            let model = modelList[index]
            navigation.tabBarItem.title = model.title
            navigation.tabBarItem.image = UIImage(named: model.iconName)?.withRenderingMode(.alwaysOriginal)
            navigation.tabBarItem.selectedImage = UIImage(named: model.iconName)?.withTintColor(RdbColor.shared.theme.color,renderingMode: .alwaysOriginal)
        }
    }
    
    func controllerWithClassName(className: String) -> UIViewController {
        switch className {
        case "RdbMarqueeViewController":
            return RdbMarqueeViewController()
        case "RdbNeonViewController":
            return RdbNeonViewController()
        case "RdbTemplateViewController":
            return RdbTemplateViewController()
        case "RdbSettingViewController":
            return RdbSettingViewController()
        default:
            return UIViewController()
        }
    }
    
    func getTabbarItems() -> Array<Any> {
        return [
            [
                "controller": "RdbMarqueeViewController",
                "iconName": "tabbar_marquee",
                "title": "Marquee"
            ],
            [
                "controller": "RdbNeonViewController",
                "iconName": "tabbar_neon",
                "title": "Neon"
            ],
            [
                "controller": "RdbTemplateViewController",
                "iconName": "tabbar_template",
                "title": "Template"
            ],
            [
                "controller": "RdbSettingViewController",
                "iconName": "tabbar_setting",
                "title": "Setting"
            ]
        ]
    }
    
    
    override var shouldAutorotate: Bool{
        return false
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .portrait
    }
    deinit{
        NotificationCenter.default.removeObserver(self)
    }
}
